package com.corx.network.config.condition;

import com.corx.network.core.storage.Storage;
import com.corx.network.core.type.StorageTypeEnum;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @author gx
 * @version 1.0.0
 */
public class MissingMySqlMessageStorageCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        if (beanFactory == null) {
            return true;
        }
        Map<String, Storage> beansOfType = beanFactory.getBeansOfType(Storage.class);
        boolean hasMySqlImpl = beansOfType.values().stream()
            .anyMatch(storage -> StorageTypeEnum.MYSQL.equals(storage.type()));
        return !hasMySqlImpl;
    }


}
